package web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
            假设username=zhangsan&password=123 表示登录成功，否则登录失败
                登录失败后，回到登录页面，并提示 “用户名或密码错误”
                成功，跳转到index.jsp
         */

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if ("zhangsan".equals(username) && "123".equals(password)){
            //重定向到首页
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
        }else{
            //转发跳转到一个登录失败的servlet中

            req.setAttribute("msg","用户名或密码错误！！！");

//            req.getRequestDispatcher("/loginFailServlet").forward(req,resp);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }
}
